<template>
    <div>
        <el-dialog :title="dialogtitle" width="1000px" class="icon-dialog" :visible.sync="show" :before-close="closeForm" append-to-body @open="open">
            <el-form ref="form" :size="size" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="名称" prop="name">
                            <!-- <el-input v-model="form.name" auto-complete="off"  clearable placeholder="请输入名称" /> -->
                            <div>{{ form.name }}</div>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="选择用户">
                            <el-select v-model="form.user" multiple filterable remote :remote-method="getInfo" :loading="loading" style="margin-left: 20px;" placeholder="请选择">
                                <el-option v-for="item in user" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="loading" type="primary" @click="submit">
                    <span v-if="!loading">确 定</span>
                    <span v-else>提 交 中...</span>
                </el-button>
                <el-button @click="closeForm">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'Coupongrant',
    components: {},
    props: {
        show: {
            type: Boolean,
            default: false
        },

        size: {
            type: String,
            default: 'small'
        },
        id: {
            type: Number
        },
    },
    data() {
        return {
            dialogtitle: '',
            form: {
                title: '',
                status: 1
            },
            loading: false,
            loadings: false,
            rules: {
                name: [{
                    required: true,
                    message: '名称不能为空',
                    trigger: 'blur'
                }]
            },
            user: []
        }
    },
    watch: {
        show(val) {

        }
    },
    methods: {
        open() {
            this.dialogtitle = '发放优惠券'
            this.getInfo()
        },
        getInfo(query) {
            if (query !== "") {
                this.loadings = true;
            }
            this.$api.post('/coupon/getInfo', {
                id: this.id,
                grant: 1,
                keyword: query
            }).then(res => {
                this.form = res.data
                this.user = res.user
                this.loadings = false
            })
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$api.post('/coupon/grant', this.form).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        })
                        this.$emit('refesh_list')
                        this.closeForm()
                    }).catch(() => {
                        this.loading = false
                    })
                }
            })
        },
        closeForm() {
            this.$emit('update:show', false)
            this.loading = false
        }
    }
}
</script>
